<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/9/17
 * Time: 16:15
 */
namespace app\admin\controller;
use think\Db;
class Feedbackcate extends Base
{
    /*
     * 分类数据
     */
    public function index()
    {
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['cate_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('feedback_cate')
                ->where($map)
                ->count();
            $lists = Db::name('feedback_cate')
                ->where($map)
                ->field('cate_id,cate_name,cate_create_time,cate_update_time')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('feedbackcate/index');
    }

    /*
     * 添加分类
     */
    public function cateAdd(){
        if(request ()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param['cate_name'] = $cate_name;
            $param['cate_create_time'] = $time;
            $param['cate_update_time'] = $time;
            $res = Db::name('feedback_cate')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch('feedbackcate/cate_add');
    }

    /*
     * 编辑分类
     */
    public function cateEdit(){
        if(request ()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param['cate_name'] = $cate_name;
            $param['cate_update_time'] = $time;
            $res = Db::name('feedback_cate')->where('cate_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $data = Db::name('feedback_cate')
            ->where('cate_id',input('id'))
            ->find();
        return $this->fetch('feedbackcate/cate_edit',['data'=>$data]);
    }

    public function cateDel(){
        extract(input());
        $res = Db::name('feedback_cate')->delete($id);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}